package com.ylsk.check.service.impl;

import java.util.Date;
import java.util.List;

import com.ylsk.check.domain.*;
import com.ylsk.check.domain.dto.RectificationReportDTO;
import com.ylsk.check.domain.so.RectificationReportSO;
import com.ylsk.check.domain.vo.RectificationReportVO;
import com.ylsk.check.mapper.CheckProblemMapper;
import com.ylsk.check.mapper.CheckRecordMapper;
import com.ylsk.common.annotation.DataScope;
import com.ylsk.common.config.SnowIdConfig;
import com.ylsk.common.exception.ServiceException;
import com.ylsk.common.utils.StringUtils;
import com.ylsk.system.domain.FileSimple;
import com.ylsk.system.helper.FileHelper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ylsk.check.mapper.RectificationReportMapper;
import com.ylsk.check.service.IRectificationReportService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 整改上报Service业务层处理
 *
 * @author yangsy
 * @date 2023-12-25
 */
@Service
public class RectificationReportServiceImpl implements IRectificationReportService
{
    @Autowired
    private RectificationReportMapper rectificationReportMapper;

    @Autowired
    private SnowIdConfig snowIdConfig;

    @Autowired
    private FileHelper fileHelper;

    @Autowired
    private ReportInfoHelper helper;

    @Autowired
    private CheckProblemMapper checkProblemMapper;

    @Autowired
    private CheckRecordMapper checkRecordMapper;

    /**
     * 查询整改上报
     *
     * @param rectificationReportId 整改上报主键
     * @return 整改上报
     */
    @Override
    public RectificationReportVO selectRectificationReportByRectificationReportId(Long rectificationReportId)
    {
        RectificationReportVO rectificationReportVO = rectificationReportMapper.selectRectificationReportByRectificationReportId(rectificationReportId);
        //构建附件信息
        Long rectificationFileId = rectificationReportVO.getRectificationFileId();
        List<FileSimple> fileSimpleList = fileHelper.getFileByBusinessId(rectificationFileId);
        rectificationReportVO.setRectificationFile(fileSimpleList);
        return rectificationReportVO;
    }

    /**
     * 查询整改上报列表
     *
     * @param rectificationReportSO 整改上报
     * @return 整改上报
     */
    @Override
    @DataScope(deptAlias = "sd",userAlias = "su")
    public List<RectificationReportVO> selectRectificationReportList(RectificationReportSO rectificationReportSO)
    {
        return rectificationReportMapper.selectRectificationReportList(rectificationReportSO);
    }

    /**
     * 新增整改上报
     *
     * @param rectificationReportDTO 整改上报
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int insertRectificationReport(RectificationReportDTO rectificationReportDTO)
    {
        RectificationReport rectificationReport = new RectificationReport();
        BeanUtils.copyProperties(rectificationReportDTO,rectificationReport);

        final Long checkProblemId = rectificationReport.getCheckProblemId();
        if(null == checkProblemId){
            throw new ServiceException("施工检查问题id不能为空!");
        }
        //检查问题id
        CheckProblem checkProblem = checkProblemMapper.selectById(checkProblemId);
        CheckRecord checkRecord = checkRecordMapper.selectById(checkProblem.getCheckRecordId());

        ReportInfo reportInfo = helper.getReportInfo(checkRecord.getReportInfoId());
        rectificationReport.setDeptId(reportInfo.getDeptId());
        rectificationReport.setReviewedBy(reportInfo.getReviewedBy());
        rectificationReport.setHead(reportInfo.getCreateBy());
        rectificationReport.setReportInfoId(reportInfo.getReportInfoId());
        rectificationReport.setRectificationTime(null == rectificationReportDTO.getRectificationTime() ? new Date() : rectificationReportDTO.getRectificationTime());

        //  附件信息
        String rectificationFile = rectificationReportDTO.getRectificationFile();
        long rectificationFileId = snowIdConfig.getSnowId();
        if(StringUtils.isNotEmpty(rectificationFile)){
            fileHelper.saveBusinessFileData(rectificationFile,rectificationFileId);
        }

        rectificationReport.setRectificationReportId(snowIdConfig.getSnowId());
        rectificationReport.setRectificationFileId(rectificationFileId);
        return rectificationReportMapper.insert(rectificationReport);
    }

    /**
     * 修改整改上报
     *
     * @param rectificationReportDTO 整改上报
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int updateRectificationReport(RectificationReportDTO rectificationReportDTO)
    {
        final Long rectificationReportId = rectificationReportDTO.getRectificationReportId();
        if(null == rectificationReportId){
            throw new ServiceException("rectificationReportId 不能为空!");
        }
        if(null == rectificationReportDTO.getRectificationFile()){
            throw new ServiceException("附件id不能 不能为空!");
        }
        RectificationReport rectificationReport = new RectificationReport();
        BeanUtils.copyProperties(rectificationReportDTO,rectificationReport);
        //更新附件信息
        String rectificationFile = rectificationReportDTO.getRectificationFile();
        RectificationReport _rectificationReport = rectificationReportMapper.selectById(rectificationReportId);
        fileHelper.updateBusinessFileData(rectificationFile,_rectificationReport.getRectificationFileId());
        return rectificationReportMapper.updateById(rectificationReport);
    }

    /**
     * 批量删除整改上报
     *
     * @param rectificationReportIds 需要删除的整改上报主键
     * @return 结果
     */
    @Override
    public int deleteRectificationReportByRectificationReportIds(Long[] rectificationReportIds)
    {
        return rectificationReportMapper.deleteRectificationReportByRectificationReportIds(rectificationReportIds);
    }

    /**
     * 删除整改上报信息
     *
     * @param rectificationReportId 整改上报主键
     * @return 结果
     */
    @Override
    public int deleteRectificationReportByRectificationReportId(Long rectificationReportId)
    {
        return rectificationReportMapper.deleteRectificationReportByRectificationReportId(rectificationReportId);
    }
}
